package com.mfangsoft.zhuangjialong.integration.customer.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mfangsoft.zhuangjialong.app.weixin.mapper.WeiXinMapper;
import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.integration.customer.mapper.CustomerMapper;
import com.mfangsoft.zhuangjialong.integration.customer.mapper.CustomerSourceMapper;
import com.mfangsoft.zhuangjialong.integration.customer.model.CustomerAddress;
import com.mfangsoft.zhuangjialong.integration.customer.model.CustomerInfo;
import com.mfangsoft.zhuangjialong.integration.customer.model.CustomerSource;
import com.mfangsoft.zhuangjialong.integration.customer.service.ICustomerService;
import com.mfangsoft.zhuangjialong.integration.user.model.UserEntity;
import com.mfangsoft.zhuangjialong.integration.usermanage.mapper.SellerPointEntityMapper;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.SellerPointEntity;

@Service
public class CustomerServiceImpl implements ICustomerService {
	
	@Autowired
	CustomerMapper customerMapper;
	@Autowired
	WeiXinMapper weiXinMapper;
	@Autowired
	private SellerPointEntityMapper sellerPointEntityMapper;
	@Autowired
	private CustomerSourceMapper customerSourceMapper;
	
	@Override
	public Page<Map<String,Object>> selectCustomerPage(Page<Map<String,Object>> page) {
		UserEntity user = UserContext.getCurrentUser();
//		UserEntity user=new UserEntity();
//		user.setId(1020l);
//		user.setUser_level(1);
		if(user!=null){
			Map<String,Object> map=(Map<String, Object>) page.getParam();
			if(map!=null){
				map.put("level", user.getUser_level());
				map.put("shopId",user.getId());
			}else{
				map=new HashMap<String, Object>();
				map.put("level", user.getUser_level());
				map.put("shopId",user.getId());
			}
			page.setParam(map);
		}
		List<Map<String,Object>> customerPage = customerMapper.selectCustomerPage(page);
		
		List<Long> list = new ArrayList<Long>();
		
		for(Map<String,Object> map : customerPage){
			if(map.get("provinceId") !=null){
				list.add(Long.parseLong(map.get("provinceId").toString()));
			}
			if(map.get("cityId") !=null){
				list.add(Long.parseLong(map.get("cityId").toString()));
			}
		}
		if(list.size() >0){
			List<Map<String,Object>> list1 =  customerMapper.selectEareById(list);
			if(list1 != null && list1.size()>0){
				for(Map<String,Object> map1 : list1){
					for(Map<String,Object> map : customerPage){
						if(map.get("provinceId") !=null){
							if(map.get("provinceId").toString().equals(map1.get("code").toString())){
								map.put("provinceName", map1.get("name"));
							}
						}
						if(map.get("cityId") !=null){
							if(map.get("cityId").toString().equals(map1.get("code").toString())){
								map.put("cityName", map1.get("name"));
							}
						}
					}
				}
			}
		}
		
		
//		if(customerPage!=null && customerPage.size()>0){
			page.setData(customerPage);
//		}
		return page;
	}
	
	

	@Override
	public CustomerInfo selectCustomerDetail(Map<String, Object> map) {
		CustomerInfo customerDetail = customerMapper.selectCustomerDetail(map);
		if(customerDetail!=null){
			List<CustomerAddress> customerAddress = customerMapper.selectCustomerAddress(Integer.valueOf(map.get("customerId").toString()));
			customerDetail.setCustomerAddressList(customerAddress);
			String codes = customerDetail.getRegion_code();
			if(codes!=null && !"".equals(codes)){
				String[] split = codes.split(",");
				StringBuffer sb=new StringBuffer();
				for(int x=0;x<split.length;x++){
					if(x==0){
						sb.append(customerMapper.selectCodeName(split[x]));
					}else{
						sb.append(" "+customerMapper.selectCodeName(split[x]));
					}
				}
				sb.append(" "+customerDetail.getAddress());
				customerDetail.setAddress(sb.toString());
			}
		}
		return customerDetail;
	}

	@Override
	public void addCustomerSeller(Map<String, Object> map) {
		UserEntity user = UserContext.getCurrentUser();
		if(user!=null){
			map.put("shopId", user.getId());
			customerMapper.addCustomerSeller(map);
		}
	}



	@Override
	public List<Map<String, Object>> selectShopSeller(Integer shopId) {
		List<Map<String,Object>> shopSeller = customerMapper.selectShopSeller(shopId);
		return shopSeller;
	}



	@Override
	public void updateCustomerSeller(Map<String, Object> map) {
		Integer customerCount = weiXinMapper.selectSellerCustomerCount(Integer.valueOf(map.get("sellerId").toString()));
		SellerPointEntity spe=new SellerPointEntity();
		if(customerCount>0){
			spe.setPoint(5);
		}else{
			spe.setPoint(10);
		}
		spe.setSeller_id(Long.valueOf(map.get("sellerId").toString()));
		spe.setTitle("客户积分");
		spe.setType_index(3);
		spe.setType_name("客户");
		sellerPointEntityMapper.insertSelective(spe);
		customerMapper.updateCustomerSeller(map);
		customerMapper.updateCustomerOrderSeller(map);
	}



	@Override
	public Map<String, Object> selectCustomerInstallAddress(Integer customerId) {
		Map<String, Object> installAddress = customerMapper.selectCustomerInstallAddress(customerId);
		String codes=installAddress.get("region_code")+"";
		if(codes!=null && !"".equals(codes) && !"null".equals(codes)){
			String[] split = codes.split(",");
			Map<String,Object> map=new HashMap<>();
			StringBuffer sb=new StringBuffer();
			for(int x=0;x<split.length;x++){
				if(x==0){
					sb.append(customerMapper.selectCodeName(split[x]));
				}else{
					sb.append(" "+customerMapper.selectCodeName(split[x]));
				}
			}
			sb.append(" "+installAddress.get("address"));
			map.put("address", sb);
			map.put("customerId", customerId);
			UserEntity user = UserContext.getCurrentUser();
			String regionCodeByShopId = customerMapper.selectRegionCodeByShopId(Integer.valueOf(user.getId().toString()));
			map.put("region_code", regionCodeByShopId);
			return map;
		}else{
			Map<String,Object> map=new HashMap<>();
			return map;
		}
		
	}



	@Override
	public List<Map<String, Object>> selectFilialeByCode(String region_code) {
		return customerMapper.selectFilialeByCode(region_code);
	}



	@Override
	public Integer addFiliale(Map<String, Object> map) {
		return customerMapper.addCustomerSeller(map);
	}



	@Override
	public List<Map<String, Object>> selectShopByCode(String region_code) {
		UserEntity user = UserContext.getCurrentUser();
		Map<String,Object> map=new HashMap<>();
		map.put("node2", user.getId());
		map.put("region_code", region_code);
		return customerMapper.selectShopByCode(map);
	}



	@Override
	public Integer addCustomerSource(CustomerSource customerSource) {
		customerSource.setCreate_time(new Date());
		customerSource.setStatus(1);
		return customerSourceMapper.insertSelective(customerSource);
	}

	@Override
	public Integer updateCustomerSource(CustomerSource customerSource) {
		return customerSourceMapper.updateByPrimaryKeySelective(customerSource);
	}

	@Override
	public Integer deleteCustomerSource(Integer customerSourceId) {
		CustomerSource customerSource = new CustomerSource();
		customerSource.setId(customerSourceId);
		customerSource.setStatus(0);
		return customerSourceMapper.updateByPrimaryKeySelective(customerSource);
	}



	@Override
	public List<CustomerSource> selectCustomerSourceList() {
		return customerSourceMapper.selectCustomerSourceList();
	}



	@Override
	public Integer updateCustomerProperty(Map<String, Object> map) {
		Integer integer = customerMapper.updateCustomerProperty(map);
		return integer;
	}



	@Override
	public Map<String, Object> selectCustomerProperty(Integer customerId) {
		Map<String, Object> map = customerMapper.selectCustomerProperty(customerId);
		return map;
	}

}
 